package syscore_service

import (
	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/shopspring/decimal"
)

type GetGoods struct {
	InstitutionID int
	PageNum       int
	PageSize      int
}

func (t *GetGoods) GetGoods() (interface{}, error) {
	maps := map[string]interface{}{}
	total, _ := models.GetGoodsTotal(maps)
	goodsList, _ := models.GetGoodsPage([]string{"goods_id", "user_id", "supply_price", "cost_price", "streamer_id", "goods_name", "goods_spec", "goods_no", "selling_price", "goods_img", "create_time", "goods_num"}, maps, t.PageNum, t.PageSize, "create_time desc")
	var lists []interface{}
	for _, v := range goodsList {
		sellingPrice := decimal.NewFromInt(int64(v.SellingPrice)).Div(decimal.NewFromInt(100))
		costPrice := decimal.NewFromInt(int64(v.CostPrice)).Div(decimal.NewFromInt(100))
		supplyPrice := decimal.NewFromInt(int64(v.SupplyPrice)).Div(decimal.NewFromInt(100))
		var goods_tag []interface{}
		for _, v1 := range v.GoodsTag {
			var tag = map[string]interface{}{
				"tag_id":   v1.TagID,
				"tag_name": v1.Tag.TagName,
			}
			goods_tag = append(goods_tag, tag)
		}
		params := map[string]interface{}{
			"goods_id":        v.GoodsID,
			"goods_name":      v.GoodsName,
			"goods_spec":      v.GoodsSpec,
			"goods_no":        v.GoodsNo,
			"selling_price":   sellingPrice,
			"supply_price":    supplyPrice,
			"cost_price":      costPrice,
			"create_time":     v.CreateTime.Format("2006-01-02 15:04:05"),
			"goods_img":       v.GoodsImg,
			"goods_num":       v.GoodsNum,
			"streamer_name":   v.Streamer.StreamerName,
			"streamer_mobile": v.Streamer.StreamerMobile,
			"user_name":       v.User.UserName,
			"goods_tag":       goods_tag,
		}
		lists = append(lists, params)
	}
	return map[string]interface{}{
		"lists": lists,
		"total": total,
	}, nil
}
